<template>
  <!-- <el-dialog title="新建实体类" top="5vh" width="60%"  :center="true" :lock-scroll="true" :visible.sync="shiTiLeiShow" @close="close">

  </el-dialog>-->

  <el-card class="box-card" shadow="never">
    <div slot="header" class="clearfix">
      <span>新建实体类</span>
    </div>
    <div>
      <el-form
        ref="addShiTiLInfo"
        :model="addShiTiLInfo"
        :rules="rules"
        label-width="100px"
        class="demo-mecAddData"
      >
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="实体类名称:" prop="shiTiLMC">
              <el-input v-model="addShiTiLInfo.shiTiLMC" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="实体类代号:" prop="shiTiLDH">
              <el-input v-model="addShiTiLInfo.shiTiLDH" />
            </el-form-item>
          </el-col>
          <!--          <el-col :span="6">-->
          <!--            <el-form-item label="实体类类别:" prop="shiTiLLB">-->
          <!--              <el-select v-model="addShiTiLInfo.shiTiLLB" value-key="id" placeholder="请选择">-->
          <!--                <el-option-->
          <!--                  v-for="item in shiTiLLB"-->
          <!--                  :key="item.value"-->
          <!--                  :label="item.name"-->
          <!--                  :value="item"-->
          <!--                />-->
          <!--              </el-select>-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
          <!--          <el-col :span="6">-->
          <!--            <el-form-item label="应用系统:" prop="yingYongXT">-->
          <!--              <el-select v-model="addShiTiLInfo.yingYongXT" value-key="id" placeholder="请选择">-->
          <!--                <el-option-->
          <!--                  v-for="item in yingYongXT"-->
          <!--                  :key="item.value"-->
          <!--                  :label="item.name"-->
          <!--                  :value="item"-->
          <!--                />-->
          <!--              </el-select>-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
          <el-col :span="24">
            <el-form-item label="备注:" prop="beiZhu">
              <el-input
                v-model="addShiTiLInfo.beiZhu"
                type="textarea"
                maxlength="200"
                show-word-limit
                :autosize="{ minRows: 2, maxRows: 6 }"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-link type="primary" @click="addRow">新增</el-link>
        <template>
          <el-table
            border
            :data="addShiTiLInfo.zds"
            style="width: 100%;"
            :height="screenHeight - 410"
            size="small"
            :max-height="screenHeight"
            class="ziduan dept-content"
          >
            <el-table-column type="index" label="序号" width="55" fixed />
            <el-table-column prop="ziDuanDH" label="字段代号">
              <template slot-scope="scope">
                <el-form-item :prop="'zds.' + scope.$index + '.ziDuanDH'" :rules="rules.ziDuanDH">
                  <el-input v-model="scope.row.ziDuanDH" size="mini" />
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column prop="ziDuanMC" label="字段名称">
              <template slot-scope="scope">
                <el-form-item :prop="'zds.' + scope.$index + '.ziDuanMC'" :rules="rules.ziDuanMC">
                  <el-input v-model="scope.row.ziDuanMC" size="mini" />
                </el-form-item>
              </template>
            </el-table-column>

            <el-table-column prop="kuanDu" label="宽度">
              <template slot-scope="scope">
                <el-form-item :prop="'zds.' + scope.$index + '.kuanDu'" :rules="rules.kuanDu">
                  <el-input v-model="scope.row.kuanDu" size="mini" />
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column prop="jingDu" label="精度">
              <template slot-scope="scope">
                <el-form-item :prop="'zds.' + scope.$index + '.jingDu'" :rules="rules.jingDu">
                  <el-input v-model="scope.row.jingDu" size="mini" />
                </el-form-item>
              </template>
            </el-table-column>

            <!--            <el-table-column prop="ziDuanZLX" label="字段值类型">-->
            <!--              <template slot-scope="scope">-->
            <!--                <el-form-item :prop="'zds.' + scope.$index + '.ziDuanZLX'" :rules="rules.ziDuanZLX">-->
            <!--                  <el-select-->
            <!--                    v-model="scope.row.ziDuanZLX"-->
            <!--                    size="small"-->
            <!--                    value-key="id"-->
            <!--                    filterable-->
            <!--                    placeholder="请选择"-->
            <!--                  >-->
            <!--                    <el-option-->
            <!--                      v-for="item in ziDuanZLX"-->
            <!--                      :key="item.id"-->
            <!--                      :label="item.name"-->
            <!--                      :value="item"-->
            <!--                    />-->
            <!--                  </el-select>-->
            <!--                </el-form-item>-->
            <!--              </template>-->
            <!--            </el-table-column>-->
            <!--            <el-table-column prop="guanLianSTL" label="关联实体类" width="160px">-->
            <!--              <template slot-scope="scope">-->
            <!--                <el-form-item-->
            <!--                  :prop="'zds.' + scope.$index + '.guanLianSTL'"-->
            <!--                  :rules="rules.guanLianSTL"-->
            <!--                >-->
            <!--                  <el-select-->
            <!--                    v-model="scope.row.guanLianSTL"-->
            <!--                    size="small"-->
            <!--                    value-key="id"-->
            <!--                    filterable-->
            <!--                    placeholder="请选择"-->
            <!--                  >-->
            <!--                    <el-option-->
            <!--                      v-for="item in guanLianSTL"-->
            <!--                      :key="item.id"-->
            <!--                      :label="item.name"-->
            <!--                      :value="item"-->
            <!--                    />-->
            <!--                  </el-select>-->
            <!--                </el-form-item>-->
            <!--              </template>-->
            <!--            </el-table-column>-->
            <!--            <el-table-column prop="jiLianFS" label="级联方式" width="140px">-->
            <!--              <template slot-scope="scope">-->
            <!--                <el-form-item :prop="'zds.' + scope.$index + '.jiLianFS'" :rules="rules.jiLianFS">-->
            <!--                  <el-select v-model="scope.row.jiLianFS" size="small" value-key="id" filterable placeholder="请选择">-->
            <!--                    <el-option v-for="item in jiLianFS" :key="item.id" :label="item.name" :value="item" />-->
            <!--                  </el-select>-->
            <!--                </el-form-item>-->
            <!--              </template>-->
            <!--            </el-table-column>-->
            <!--            <el-table-column prop="fuJianLX" label="附件类型" width="140px">-->
            <!--              <template slot-scope="scope">-->
            <!--                <el-form-item :prop="'zds.' + scope.$index + '.fuJianLX'" :rules="rules.fuJianLX">-->
            <!--                  <el-select v-model="scope.row.fuJianLX" size="small" value-key="id" filterable placeholder="请选择">-->
            <!--                    <el-option v-for="item in fuJianLX" :key="item.id" :label="item.name" :value="item" />-->
            <!--                  </el-select>-->
            <!--                </el-form-item>-->
            <!--              </template>-->
            <!--            </el-table-column>-->
            <!--            <el-table-column prop="guanLianFLZD" label="关联父类字段" width="150px">-->
            <!--              <template slot-scope="scope">-->
            <!--                <el-form-item :prop="'zds.' + scope.$index + '.guanLianFLZD'" :rules="rules.guanLianFLZD">-->
            <!--                  <el-select v-model="scope.row.guanLianFLZD" size="small" value-key="id" filterable placeholder="请选择">-->
            <!--                    <el-option v-for="item in guanLianFLZD" :key="item.id" :label="item.name" :value="item" />-->
            <!--                  </el-select>-->
            <!--                </el-form-item>-->
            <!--              </template>-->
            <!--            </el-table-column>-->
            <el-table-column fixed="right" label="操作" width="100">
              <template slot-scope="scope">
                <!-- <el-form-item> -->
                <!-- <el-button
                    type="warning"
                    size="small"
                    @click.native.prevent="deleteRow(scope.$index, shiTiLInfoEdit.ziDuan)"
                  >删除
                  </el-button> -->
                <el-link
                  type="danger"
                  @click.native.prevent="deleteRow(scope.$index, addShiTiLInfo.zds)"
                >移除</el-link>
                <!-- </el-form-item> -->
              </template>
            </el-table-column>
          </el-table>
        </template>
        <!-- <el-form-item>
        <el-button type="primary" @click="submitForm">保存</el-button>
        <el-button @click="close">取消</el-button>
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">保存</el-button>
        <el-button @click="resetForm">重置</el-button>
        <el-button @click="close">取消</el-button>
      </div>
    </div>
  </el-card>
</template>

<script>
import { LiYuDataSource } from 'liyu-ui'

let shiTiLeiDatasource
export default {
  components: {},
  props: {  },
  data() {
    return {
      infiledList: [],

      shiTiL: [
        { name: '实体类一', id: 1 },
        { name: '实体类二', id: 2 }
      ],
      ziDuanZLX: [
        { name: '字段类型一', id: 1 },
        { name: '字段类型二', id: 2 }
      ],
      guanLianSTL: [
        { name: '关联实体类一', id: 1 },
        { name: '关联实体类二', id: 2 }
      ],
      jiLianFS: [
        { name: '设置为空', id: 1 },
        { name: '限制删除', id: 2 },
        { name: '级联删除', id: 3 }
      ],
      fuJianLX: [
        { name: '级联方式一', id: 1 },
        { name: '级联方式二', id: 2 }
      ],
      guanLianFLZD: [
        { name: '关联一', id: 1 },
        { name: '关联二', id: 2 }
      ],
      yingYongXT: [
        { name: '系统一', id: 1 },
        { name: '系统二', id: 2 }
      ],
      shiTiLLB: [
        { name: '类别一', id: 1 },
        { name: '类别二', id: 2 }
      ],
      shiTiLeiShow: false,
      FromLoading: true,
      screenHeight: document.body.clientHeight,
      addShiTiLInfo: {},
      rules: {
        shiTiLDH: [{ required: true, message: '请输入', trigger: 'blur' }],
        shiTiLMC: [{ required: true, message: '请输入', trigger: 'blur' }],
        shiTiLLB: [{ required: true, message: '请选择', trigger: 'change' }],
        yingYongXT: [{ required: true, message: '请输入内容', trigger: 'blur' }],
        beiZhu: [{ required: true, message: '请输入', trigger: 'blur' }],
        ziDuanDH: [{ required: true, message: '请输入', trigger: 'blur' }],
        ziDuanMC: [{ required: true, message: '请输入', trigger: 'blur' }],
        lieMing: [{ required: true, message: '请输入', trigger: 'blur' }],
        kuanDu: [{ required: false, message: '请输入', trigger: 'blur' }],
        jingDu: [{ required: false, message: '请输入', trigger: 'blur' }],
        shiTiL: [{ required: true, message: '请选择', trigger: 'change' }],
        ziDuanZLX: [{ required: true, message: '请选择', trigger: 'change' }],
        guanLianSTL: [{ required: true, message: '请选择', trigger: 'change' }],
        jiLianFS: [{ required: true, message: '请选择', trigger: 'change' }],
        guanLianFLZD: [{ required: true, message: '请选择', trigger: 'change' }]
      }
    }
  },
  watch: {
    screenHeight(val) {
      // 为了避免频繁触发resize函数导致页面卡顿，使用定时器
      if (!this.timer) {
        // 一旦监听到的screenWidth值改变，就将其重新赋给data里的screenWidth
        this.screenHeight = val
        this.timer = true
        const that = this
        setTimeout(function() {
          // 打印screenWidth变化的值
          that.timer = false
        }, 400)
      }
    },
    shiTiLInfo: function(newVla, oldVal) {
      // console.log(newVla)
      this.addShiTiLInfo = newVla
    }
  },
  created() {
    this.initPage()
  },
  mounted() {
    const that = this
    window.onresize = () => {
      return (() => {
        window.screenHeight = document.body.clientHeight
        that.screenHeight = window.screenHeight
      })()
    }
  },

  methods: {
    initPage() {
      shiTiLeiDatasource = new LiYuDataSource({
        shiTiLeiDH: 'DEV_ShiTiLei'
      })
      console.log(shiTiLeiDatasource)
      const recordSet = shiTiLeiDatasource.loadData([{
        shiTiLDH: null,
        shiTiLMC: null,
        // shiTiLLB: null,
        // yingYongXT: null,
        beiZhu: null,
        zds: []
      }])
      this.addShiTiLInfo = recordSet.getRecordByIndex(0)
    },
    deleteRow(index, rows) {
      // 删除改行
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          rows.splice(index, 1)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    addRow() {
      this.addShiTiLInfo.zds.push({
        ziDuanDH: null,
        ziDuanMC: null,
        kuanDu: null,
        jingDu: null,
        ziDuanZLX: null,
        // guanLianSTL: null,
        // jiLianFS: null,
        // fuJianLX:null,
        // guanLianFLZD: null,
        __stat: 'new'
      })
    },
    submitForm() {
      this.$refs.addShiTiLInfo.validate(valid => {
        if (valid) {
          shiTiLeiDatasource.submit()
        } else {
          return false
        }
      })
    },
    close() {
      this.shiTiLeiShow = false
      this.$refs.addShiTiLInfo.resetFields()
    },
    resetForm() {
      this.$refs.addShiTiLInfo.resetFields()
    },
    OpenAddShiTiLei() {
      this.shiTiLeiShow = true
    }
  }
}
</script>

<style scoped>
  .el-card {
    border-top: none !important;
  }

  /*修改滚动条样式*/
  div::-webkit-scrollbar {
    width: 10px;
    height: 10px;
    /**/
  }

  div::-webkit-scrollbar-track {
    background: rgb(239, 239, 239);
  }

  div::-webkit-scrollbar-thumb {
    background: #bfbfbf;
    border-radius: 10px;
  }

  /*修改滚动条样式*/
  .ziduan >>> div::-webkit-scrollbar {
    width: 10px;
    height: 10px;
    /**/
  }

  .ziduan >>> div::-webkit-scrollbar-track {
    background: rgb(239, 239, 239);
  }

  .ziduan >>> div::-webkit-scrollbar-thumb {
    background: #bfbfbf;
    border-radius: 10px;
  }

  .ziduan >>> .el-form-item__content {
    margin-left: 0px !important;
  }

  .basic-info-body {
    /* height: 600px; */
    overflow-y: scroll;
    overflow-x: scroll;
  }

  .dialog-footer {
    display: flex;
    justify-content: center;
    margin-top: 20px;
  }
</style>
